/*
 * FindReplacePanel
 * Copyright (C) 2011, Aleksey Nikiforov
 *
 * This file is part of FindReplacePanel.
 *
 * FindReplacePanel is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FindReplacePanel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package simplex3d.console.findreplace;

import java.awt.FocusTraversalPolicy;
import java.awt.Container;
import java.awt.Component;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


/**
 *
 * @author Aleksey Nikiforov (lex)
 */
class ReplaceComponent extends JPanel {

    private FocusTraversalPolicy traversalPolicy;


    /** Creates new form ReplacePanel */
    public ReplaceComponent(Map<String, Action> optionsActions, Map<String, Action> findReplaceActions) {
        final List<Component> traversalOrder = new ArrayList<Component>(7);

        traversalPolicy = new FocusTraversalPolicy() {
            @Override
            public Component getComponentAfter(Container aContainer, Component aComponent) {
                int index = traversalOrder.indexOf(aComponent) + 1;
                if (index >= traversalOrder.size()) index = 0;
                return traversalOrder.get(index);
            }

            @Override
            public Component getComponentBefore(Container aContainer, Component aComponent) {
                int index = traversalOrder.indexOf(aComponent) - 1;
                if (index < 0) index = traversalOrder.size() - 1;
                return traversalOrder.get(index);
            }

            @Override
            public Component getFirstComponent(Container aContainer) {
                return traversalOrder.get(0);
            }

            @Override
            public Component getLastComponent(Container aContainer) {
                return traversalOrder.get(traversalOrder.size() - 1);
            }

            @Override
            public Component getDefaultComponent(Container aContainer) {
                return getFirstComponent(aContainer);
            }
        };

        initComponents();

        javax.swing.GroupLayout layout = (javax.swing.GroupLayout) getLayout();
        OptionsComponent optionsPanel = new OptionsComponent(optionsActions);
        layout.replace(this.optionsPanel, optionsPanel);
        this.optionsPanel = optionsPanel;

        traversalOrder.add(findComboBox.getEditor().getEditorComponent());
        traversalOrder.add(replaceComboBox.getEditor().getEditorComponent());
        traversalOrder.add(findNextButton);
        traversalOrder.add(findPreviousButton);
        traversalOrder.add(replaceButton);
        traversalOrder.add(replaceAllButton);
        traversalOrder.add(optionsPanel.getMatchCaseCheckBox());
        traversalOrder.add(optionsPanel.getWholeWordsCheckBox());
        traversalOrder.add(optionsPanel.getRegexCheckBox());
        traversalOrder.add(optionsPanel.getHighlightCheckBox());
        traversalOrder.add(optionsPanel.getHideButton());

        findNextButton.setAction(findReplaceActions.get(FindReplacePanel.CMD_FIND_NEXT));
        findPreviousButton.setAction(findReplaceActions.get(FindReplacePanel.CMD_FIND_PREVIOUS));
        replaceButton.setAction(findReplaceActions.get(FindReplacePanel.CMD_REPLACE));
        replaceAllButton.setAction(findReplaceActions.get(FindReplacePanel.CMD_REPLACE_ALL));
    }

    
    public JComboBox getFindComboBox() {
        return findComboBox;
    }

    public JComboBox getReplaceComboBox() {
        return replaceComboBox;
    }


    public void addActionListener(ActionListener a) {
        ((OptionsComponent) optionsPanel).addActionListener(a);

        findNextButton.addActionListener(a);
        findPreviousButton.addActionListener(a);
        replaceButton.addActionListener(a);
        replaceAllButton.addActionListener(a);
    }

    public void removeActionListener(ActionListener a) {
        ((OptionsComponent) optionsPanel).removeActionListener(a);

        findNextButton.removeActionListener(a);
        findPreviousButton.removeActionListener(a);
        replaceButton.removeActionListener(a);
        replaceAllButton.removeActionListener(a);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        findLabel = new javax.swing.JLabel();
        replaceLabel = new javax.swing.JLabel();
        findNextButton = new javax.swing.JButton();
        findPreviousButton = new javax.swing.JButton();
        replaceButton = new javax.swing.JButton();
        replaceAllButton = new javax.swing.JButton();
        findComboBox = new javax.swing.JComboBox();
        replaceComboBox = new javax.swing.JComboBox();
        optionsPanel = new javax.swing.JPanel();

        setFocusCycleRoot(true);
        setFocusTraversalPolicy(traversalPolicy);

        findLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("simplex3d/console/findreplace/Bundle"); // NOI18N
        findLabel.setText(bundle.getString("FindReplaceComponent.findLabel.text")); // NOI18N

        replaceLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        replaceLabel.setText(bundle.getString("ReplaceComponent.replaceLabel.text")); // NOI18N

        findNextButton.setMnemonic(java.util.ResourceBundle.getBundle("simplex3d/console/findreplace/Bundle").getString("FindReplaceComponent.findNextButton.mnemonic").charAt(0));
        findNextButton.setText(bundle.getString("FindReplaceComponent.findNextButton.text")); // NOI18N

        findPreviousButton.setMnemonic(java.util.ResourceBundle.getBundle("simplex3d/console/findreplace/Bundle").getString("FindReplaceComponent.findPreviousButton.mnemonic").charAt(0));
        findPreviousButton.setText(bundle.getString("FindReplaceComponent.findPreviousButton.text")); // NOI18N

        replaceButton.setMnemonic(java.util.ResourceBundle.getBundle("simplex3d/console/findreplace/Bundle").getString("ReplaceComponent.replaceButton.mnemonic").charAt(0));
        replaceButton.setText(bundle.getString("ReplaceComponent.replaceButton.text")); // NOI18N

        replaceAllButton.setMnemonic(java.util.ResourceBundle.getBundle("simplex3d/console/findreplace/Bundle").getString("ReplaceComponent.replaceAllButton.mnemonic").charAt(0));
        replaceAllButton.setText(bundle.getString("ReplaceComponent.replaceAllButton.text")); // NOI18N

        findComboBox.setEditable(true);
        findComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "" }));

        replaceComboBox.setEditable(true);

        javax.swing.GroupLayout optionsPanelLayout = new javax.swing.GroupLayout(optionsPanel);
        optionsPanel.setLayout(optionsPanelLayout);
        optionsPanelLayout.setHorizontalGroup(
            optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 660, Short.MAX_VALUE)
        );
        optionsPanelLayout.setVerticalGroup(
            optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 70, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(replaceLabel)
                    .addComponent(findLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(replaceComboBox, 0, 390, Short.MAX_VALUE)
                    .addComponent(findComboBox, 0, 390, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(findNextButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(replaceButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(replaceAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(findPreviousButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(optionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(findPreviousButton)
                    .addComponent(findNextButton)
                    .addComponent(findComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(findLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(replaceButton)
                    .addComponent(replaceComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(replaceAllButton)
                    .addComponent(replaceLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(optionsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox findComboBox;
    private javax.swing.JLabel findLabel;
    private javax.swing.JButton findNextButton;
    private javax.swing.JButton findPreviousButton;
    private javax.swing.JPanel optionsPanel;
    private javax.swing.JButton replaceAllButton;
    private javax.swing.JButton replaceButton;
    private javax.swing.JComboBox replaceComboBox;
    private javax.swing.JLabel replaceLabel;
    // End of variables declaration//GEN-END:variables

}
